/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package entityopinions;

import com.aliasi.chunk.Chunker;
import com.aliasi.classify.LMClassifier;
import com.aliasi.crf.ChainCrfChunker;
import com.aliasi.crf.ChainCrfChunkerLEX;
import com.aliasi.hmm.HiddenMarkovModel;
import com.aliasi.hmm.HmmDecoder;
import com.aliasi.lm.NGramProcessLM;
import com.aliasi.stats.MultivariateDistribution;
import com.aliasi.util.AbstractExternalizable;
import com.sleepycat.je.DatabaseException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 *
 * @author Diogo
 */
public class ModelSerialization {

    static void writeModel(Object obj, String modelPath) throws FileNotFoundException, IOException{
        File modelFile = new File(modelPath);
        FileOutputStream fos = new FileOutputStream(modelFile);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(obj);
    }

    static EntityOpinionSupervisedBaseline readLexSupervised(String modelPath) throws IOException, ClassNotFoundException, DatabaseException{

        ObjectInputStream ois1 = new ObjectInputStream(new FileInputStream(new File(modelPath)));
        EntityOpinionSupervisedBaseline eosb = (EntityOpinionSupervisedBaseline) ois1.readObject();
        ObjectInputStream ois2 = new ObjectInputStream(new FileInputStream(new File(eosb.PO_MODEL_PATH)));
        eosb.poModel = (LMClassifier<NGramProcessLM, MultivariateDistribution>) ois2.readObject();
        ObjectInputStream ois3 = new ObjectInputStream(new FileInputStream(new File(eosb.SUBJ_MODEL_PATH)));
        eosb.subjModel = (LMClassifier<NGramProcessLM, MultivariateDistribution>) ois3.readObject();
        eosb.cache = new Cache(new File(eosb.CACHE_PATH), 100);

        return eosb;
    }

    static EntityOpinionHMM readEntHmmClass(String modelPath) throws IOException, ClassNotFoundException{
        ObjectInputStream ois1 = new ObjectInputStream(new FileInputStream(new File(modelPath)));
        EntityOpinionHMM eoh = (EntityOpinionHMM) ois1.readObject();
        ObjectInputStream ois2 = new ObjectInputStream(new FileInputStream(new File(eoh.NER_HMM_PATH)));
        eoh.chunker = (Chunker) ois2.readObject();
        ObjectInputStream ois3 = new ObjectInputStream(new FileInputStream(new File(eoh.PO_MODEL_PATH)));
        eoh.poModel = (LMClassifier<NGramProcessLM, MultivariateDistribution>) ois3.readObject();
        ObjectInputStream ois4 = new ObjectInputStream(new FileInputStream(new File(eoh.SUBJ_MODEL_PATH)));
        eoh.subjModel = (LMClassifier<NGramProcessLM, MultivariateDistribution>) ois4.readObject();

        return eoh;
    }

    static ChainCrfChunkerLEX readCrf(String modelPath) throws IOException, ClassNotFoundException, DatabaseException{
        @SuppressWarnings("unchecked")
        ChainCrfChunkerLEX crfChunker = (ChainCrfChunkerLEX) AbstractExternalizable.readObject(new File(modelPath));
        CrfFeatureExtractor cfe = (CrfFeatureExtractor) crfChunker.ChainCrf().featureExtractor();
        cfe.mPoModel = (LMClassifier<NGramProcessLM, MultivariateDistribution>) new ObjectInputStream(new FileInputStream(new File(cfe.PO_MODEL_PATH))).readObject();
        cfe.mSubjModel = (LMClassifier<NGramProcessLM, MultivariateDistribution>) new ObjectInputStream(new FileInputStream(new File(cfe.SUBJ_MODEL_PATH))).readObject();
        cfe.mCache = new Cache(new File(cfe.CACHE_PATH), 100);
        cfe.mPosTagger = new HmmDecoder((HiddenMarkovModel) AbstractExternalizable.readObject(new File(cfe.POS_HMM_PATH)));

        return crfChunker;
    }

}
